<?php
/*
 module:		收藏管理
 create_time:	2022-03-03 17:47:44
 author:	陕西格创
 contact:
*/

namespace app\api\controller;

use app\api\service\ZhCollectionService;
use app\api\model\ZhCollection as ZhCollectionModel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
use app\api\model\Member as MemberModel;
use app\backend\model\ZhAuthentication as ZhAuthenticationModel;
use app\backend\model\SysSite as SysSiteModel;

class ZhCollection extends Common
{


    /**
     * @api             {get} /ZhCollection/index 01、首页数据列表
     * @apiGroup        ZhCollection
     * @apiVersion      1.0.0
     * @apiDescription  首页数据列表
     * @apiHeader {String} Authorization 用户授权token
     * @apiHeaderExample {json} Header-示例:
     * "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"
     * @apiParam (输入参数：) {int}            [limit] 每页数据条数（默认20）
     * @apiParam (输入参数：) {int}            [page] 当前页码
     * @apiParam (失败返回参数：) {object}        array 返回结果集
     * @apiParam (失败返回参数：) {string}        array.status 返回错误码 201
     * @apiParam (失败返回参数：) {string}        array.msg 返回错误消息
     * @apiParam (成功返回参数：) {string}        array 返回结果集
     * @apiParam (成功返回参数：) {string}        array.status 返回错误码 200
     * @apiParam (成功返回参数：) {string}        array.data 返回数据
     * @apiParam (成功返回参数：) {string}        array.data.list 返回数据列表
     * @apiParam (成功返回参数：) {string}        array.data.count 返回数据总数
     * @apiSuccessExample {json} 01 成功示例
     * {"status":"200","data":""}
     * @apiErrorExample {json} 02 失败示例
     * {"status":" 201","msg":"查询失败"}
     */
    function index()
    {
        $wxapp_id = $this->request->get('wxapp_id');
        $is_hn    = $this->request->get('is_hn');
        $uId      = $this->request->uid;
//        $uId = 347;
        if (!$wxapp_id) {
            return $this->ajaxReturn($this->errorCode, '缺少平台参数');
        }
        $wxSetting = SysSiteModel::where('wxapp_id', $wxapp_id)->find();
        if (!$wxSetting) {
            return $this->ajaxReturn($this->errorCode, '平台参数未配置');
        }

        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();
        if (!$user) {
            return $this->ajaxReturn($this->errorCode, '未知用户');
        }

        $limit = $this->request->get('limit', 20, 'intval');
        $page  = $this->request->get('page', 1, 'intval');

        // 如果是红娘marriage_seeking_id为空
        if ($is_hn == 1) {
            $where = [
                'wxapp_id'            => $wxapp_id,
                'u_id'                => $uId,
                'status'              => 1,
                'marriage_seeking_id' => 0
            ];
        } else {
            $where = [
                'wxapp_id'            => $wxapp_id,
                'u_id'                => $uId,
                'status'              => 1,
                'marriage_seeking_id' => ['>', 0]
            ];
        }

        $field   = '*';
        $orderby = 'collection_id desc';

        $res = ZhCollectionService::indexList(formatWhere($where), $field, $orderby, $limit, $page);
        foreach ($res['list'] as $k => &$v) {
            $ZhAuthenticationData = ZhAuthenticationModel::where(['authentication_id' => $v['marriage_seeking_id'], 'wxapp_id' => $wxapp_id])->find();
            if ($ZhAuthenticationData) {
                //$v['age'] = date('Y', time()) - date('Y', strtotime($ZhAuthenticationData['date_birth']));
                //            $v['head_portrait'] = $this->request->domain() . $ZhAuthenticationData['head_portrait'];
                $v['head_portrait']     = $ZhAuthenticationData['head_portrait'];
                $v['real_name']         = $ZhAuthenticationData['real_name'];
                $v['height']            = $ZhAuthenticationData['height'];
                $v['city']              = $ZhAuthenticationData['city'];
                $v['is_collection']     = 1;
                $v['authentication_id'] = $ZhAuthenticationData['authentication_id'];
            } else {
                array_splice($res['list'], $k, 1);
            }

        }
        unset($v);
        return $this->ajaxReturn($this->successCode, '返回成功', htmlOutList($res));
    }

    /**
     * @api             {get} /ZhCollection/view 02、查看详情
     * @apiGroup        ZhCollection
     * @apiVersion      1.0.0
     * @apiDescription  查看详情
     * @apiParam (输入参数：) {string}            collection_id 主键ID
     * @apiParam (输入参数：) {string}            800px 主键ID
     * @apiParam (输入参数：) {string}            500px 主键ID
     * @apiHeader {String} Authorization 用户授权token
     * @apiHeaderExample {json} Header-示例:
     * "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"
     * @apiParam (失败返回参数：) {object}        array 返回结果集
     * @apiParam (失败返回参数：) {string}        array.status 返回错误码 201
     * @apiParam (失败返回参数：) {string}        array.msg 返回错误消息
     * @apiParam (成功返回参数：) {string}        array 返回结果集
     * @apiParam (成功返回参数：) {string}        array.status 返回错误码 200
     * @apiParam (成功返回参数：) {string}        array.data 返回数据详情
     * @apiSuccessExample {json} 01 成功示例
     * {"status":"200","data":""}
     * @apiErrorExample {json} 02 失败示例
     * {"status":"201","msg":"没有数据"}
     */
    function view()
    {
//        $wxapp_id          = $this->request->get('wxapp_id');
//        $marriageSeekingId = $this->request->get('marriage_seeking_id');
//        if(!$marriageSeekingId){
//            return $this->ajaxReturn($this->errorCode,'缺少信息参数');
//        }
//        if(!$wxapp_id){
//            return $this->ajaxReturn($this->errorCode,'缺少平台参数');
//        }
//        $wxSetting = SysSiteModel::where('wxapp_id',$wxapp_id)->find();
//        if(!$wxSetting){
//            return $this->ajaxReturn($this->errorCode,'平台参数未配置');
//        }
//
//		$data  = [
//		    'wxapp_id'         => $wxapp_id,
//            'authentication_id'=> $marriageSeekingId
//        ];
//		$field = '*';
//		$res   = checkData(ZhCollectionModel::field($field)->where($data)->find());
//		return $this->ajaxReturn($this->successCode,'返回成功',$res);
    }

    /**
     * @api             {post} /ZhCollection/CancelCollection 03、取消收藏
     * @apiGroup        ZhCollection
     * @apiVersion      1.0.0
     * @apiDescription  修改状态
     * @apiParam (输入参数：) {string}            collection_id 主键ID
     * @apiHeader {String} Authorization 用户授权token
     * @apiHeaderExample {json} Header-示例:
     * "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"
     * @apiParam (失败返回参数：) {object}        array 返回结果集
     * @apiParam (失败返回参数：) {string}        array.status 返回错误码 201
     * @apiParam (失败返回参数：) {string}        array.msg 返回错误消息
     * @apiParam (成功返回参数：) {string}        array 返回结果集
     * @apiParam (成功返回参数：) {string}        array.status 返回错误码 200
     * @apiParam (成功返回参数：) {string}        array.msg 返回成功消息
     * @apiSuccessExample {json} 01 成功示例
     * {"status":"200","msg":"操作成功"}
     * @apiErrorExample {json} 02 失败示例
     * {"status":"201","msg":"操作失败"}
     */
    function CancelCollection()
    {
//        $wxapp_id  = $this->request->post('wxapp_id');
//        $uId       = $this->request->uid;
//        if(!$wxapp_id){
//            return $this->ajaxReturn($this->errorCode,'缺少平台参数');
//        }
//        $wxSetting = SysSiteModel::where('wxapp_id',$wxapp_id)->find();
//        if(!$wxSetting){
//            return $this->ajaxReturn($this->errorCode,'平台参数未配置');
//        }
//
//        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();
//        if(!$user){
//            return $this->ajaxReturn($this->errorCode,'未知用户');
//        }
//		$data['collection_id'] = $this->request->post('collection_id','','intval');
//		if(empty($data['collection_id'])){
//			throw new ValidateException('参数错误');
//		}
//		$where['collection_id'] = $data['collection_id'];
//		$where['wxapp_id']      = $wxapp_id;
//		$where['u_id']          = $uId;
//		try{
//			$res = ZhCollectionModel::where($where)->update(['status'=>'0']);
//		}catch(\Exception $e){
//			abort(config('my.error_log_code'),$e->getMessage());
//		}
//		return $this->ajaxReturn($this->successCode,'操作成功');
    }


    /**
     *喜欢我的
     */
    function getLikeMe()
    {
        $wxapp_id = $this->request->get('wxapp_id');
        $uId      = $this->request->uid;
        if (!$wxapp_id) {
            return $this->ajaxReturn($this->errorCode, '缺少平台参数');
        }
        $wxSetting = SysSiteModel::where('wxapp_id', $wxapp_id)->find();
        if (!$wxSetting) {
            return $this->ajaxReturn($this->errorCode, '平台参数未配置');
        }

        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();

        if (!$user) {
            return $this->ajaxReturn($this->errorCode, '未知用户');
        }
        $limit = $this->request->get('limit', 20, 'intval');
        $page  = $this->request->get('page', 1, 'intval');
//        'status'=> 1
        $ZhCollectionData = ZhCollectionModel::where([
            'wxapp_id'   => $wxapp_id,
            'release_id' => $uId,
            'status'     => 1
        ])
            ->paginate(['list_rows' => $limit, 'page' => $page])
            ->toArray();

        foreach ($ZhCollectionData['data'] as &$v) {
            $ZhAuthenticationData = ZhAuthenticationModel::where(['u_id' => $v['u_id'], 'wxapp_id' => $wxapp_id])->find();

            if ($ZhAuthenticationData) {
                $v['age'] = date('Y', time()) - date('Y', strtotime($ZhAuthenticationData['date_birth']));
//                $v['avatar']  = $this->request->domain() . $ZhAuthenticationData['head_portrait'];
                $v['avatar']  = $ZhAuthenticationData['head_portrait'];
                $v['name']    = $ZhAuthenticationData['real_name'];
                $v['height']  = $ZhAuthenticationData['height'];
                $v['city']    = $ZhAuthenticationData['city'];
                $v['is_auth'] = 1;
            } else {
                $userData     = MemberModel::where(['uid' => $v['u_id'], 'wxapp_id' => $wxapp_id])->find();
                $v['avatar']  = $userData['avatar'];
                $v['name']    = $userData['nickname'];
                $v['is_auth'] = 0;
            }
        }
        return $this->ajaxReturn($this->successCode, '返回成功', htmlOutList($ZhCollectionData));

    }


    /**
     * 点击收藏/取消收藏
     */
    function clickCollection()
    {
        $wxapp_id          = $this->request->post('wxapp_id');
        $uId               = $this->request->uid;
        $marriageSeekingId = $this->request->post('marriage_seeking_id');
        $releaseId         = $this->request->post('release_id', 0);
        if (!$wxapp_id) {
            return $this->ajaxReturn($this->errorCode, '缺少平台参数');
        }
        $wxSetting = SysSiteModel::where('wxapp_id', $wxapp_id)->find();
        if (!$wxSetting) {
            return $this->ajaxReturn($this->errorCode, '平台参数未配置');
        }

        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();
        if (!$user) {
            return $this->ajaxReturn($this->errorCode, '未知用户');
        }
        if (!$marriageSeekingId) {
            return $this->ajaxReturn($this->errorCode, '缺少信息参数');
        }
        //if (!$releaseId) {//红娘发布的资料，没有用户ID
        //    return $this->ajaxReturn($this->errorCode, '缺少信息参数');
        //}

        $where = [
            'wxapp_id'            => $wxapp_id,
            'u_id'                => $uId,
            'marriage_seeking_id' => $marriageSeekingId
        ];

        $ZhCollectionData = ZhCollectionModel::where($where)->find();
        if ($ZhCollectionData && $ZhCollectionData['status'] == 0) {
            $where['collection_id'] = $ZhCollectionData['collection_id'];
            $res                    = ZhCollectionModel::where($where)->update(['status' => 1]);
        } elseif (!$ZhCollectionData) {
            $where['release_id'] = $releaseId;
            $where['createtime'] = time();
            $where['status']     = 1;
            $res                 = ZhCollectionModel::insert($where);
        } elseif ($ZhCollectionData && $ZhCollectionData['status'] == 1) {
            $where['collection_id'] = $ZhCollectionData['collection_id'];
            $res                    = ZhCollectionModel::where($where)->update(['status' => 0]);
        }

        if ($res) {
            return $this->ajaxReturn($this->successCode, '操作成功');
        } else {
            return $this->ajaxReturn($this->errorCode, '操作失败');
        }
    }

    /**
     * api/ZhCollection/clickHnCollection
     * 点击收藏/取消收藏
     */
    function clickHnCollection()
    {
        $wxapp_id  = $this->request->post('wxapp_id');
        $uId       = $this->request->uid;
        $releaseId = $this->request->post('release_id');
        if (!$wxapp_id) {
            return $this->ajaxReturn($this->errorCode, '缺少平台参数');
        }
        $wxSetting = SysSiteModel::where('wxapp_id', $wxapp_id)->find();
        if (!$wxSetting) {
            return $this->ajaxReturn($this->errorCode, '平台参数未配置');
        }

        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();
        if (!$user) {
            return $this->ajaxReturn($this->errorCode, '未知用户');
        }
        if (!$releaseId) {
            return $this->ajaxReturn($this->errorCode, '缺少信息参数');
        }

        $where = [
            'wxapp_id'            => $wxapp_id,
            'u_id'                => $uId,
            'marriage_seeking_id' => 0
        ];

        $ZhCollectionData = ZhCollectionModel::where($where)->find();
        if ($ZhCollectionData && $ZhCollectionData['status'] == 0) {
            $where['collection_id'] = $ZhCollectionData['collection_id'];
            $res                    = ZhCollectionModel::where($where)->update(['status' => 1]);
        } elseif (!$ZhCollectionData) {
            $where['release_id'] = $releaseId;
            $where['createtime'] = time();
            $where['status']     = 1;
            $res                 = ZhCollectionModel::insert($where);
        } elseif ($ZhCollectionData && $ZhCollectionData['status'] == 1) {
            $where['collection_id'] = $ZhCollectionData['collection_id'];
            $res                    = ZhCollectionModel::where($where)->update(['status' => 0]);
        }

        if ($res) {
            return $this->ajaxReturn($this->successCode, '操作成功');
        } else {
            return $this->ajaxReturn($this->errorCode, '操作失败');
        }
    }


    // 获取喜欢我的和我喜欢的数量
    function getLike()
    {
        $wxapp_id = $this->request->get('wxapp_id');
        $uId      = $this->request->uid;
        if (!$wxapp_id) {
            return $this->ajaxReturn($this->errorCode, '缺少平台参数');
        }
        $wxSetting = SysSiteModel::where('wxapp_id', $wxapp_id)->find();
        if (!$wxSetting) {
            return $this->ajaxReturn($this->errorCode, '平台参数未配置');
        }

        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();
        if (!$user) {
            return $this->ajaxReturn($this->errorCode, '未知用户');
        }
//        $myLikeNum = ZhCollectionModel::where(['wxapp_id'=> $wxapp_id,'u_id'=>$uId,'status'=>1])
//                                      ->count('collection_id');
        $data      = ZhCollectionModel::where(['wxapp_id' => $wxapp_id, 'u_id' => $uId, 'status' => 1])
            ->select()->toArray();
        $myLikeNum = 0;
//        var_dump($data);die;
        foreach ($data as $v) {
            $ZhAuthenticationData = ZhAuthenticationModel::where(['authentication_id' => $v['marriage_seeking_id'], 'wxapp_id' => $wxapp_id])->find();
            if ($ZhAuthenticationData) {
                $myLikeNum++;
            }
        }

        $likeMeNum          = ZhCollectionModel::where(['wxapp_id' => $wxapp_id, 'release_id' => $uId, 'status' => 1])
            ->count('collection_id');
        $res['my_like_num'] = $myLikeNum;
        $res['like_me']     = $likeMeNum;
        return $this->ajaxReturn($this->successCode, '操作成功', $res);
    }


}

